<?php
namespace Home\Controller;
use Think\Controller;
use  Think\Model;
defined('IN_ECTOUCH') or die('Deny Access');
class UsersController extends CommonController {
	  protected $user_id;
	  protected $mobile_phone;
    protected $token;
    protected $info;

    public function index(){
      return_data(20002,'功能屏蔽');
    }

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();

       $this->info=$this->common_info;
        // 验证登录
        $this->check_login();
    }
     private function check_login() {
        //检测是否登录 token是否过期
        $Users=D('Users');
        $token=I('post.token');
        
        
        $info=$this->info;

        if(!$info['user_id'])return_data(20000,'需要先登录',$_POST);
        else
        {
            $this->info=$info;
            $this->user_id=$info['user_id'];
            $this->mobile_phone=$info['mobile_phone'];
            $this->token=$info['token'];
        }

     }
   //检测验证码合法性
      private function check_sms_mobile_code($mobile_phone,$sms_mobile_code) {
         //检测短信验证
         if ( !$sms_mobile_code) {
                return_data(40003,'验证码为空');
         }
         $user_info=M("sms_mobile")->field('mobile_code,ctime')->where("mobile_phone='$mobile_phone'")->order("ctime desc")->find();
         if(!$user_info)return_data(40004,'手机不存在');
         if($user_info["mobile_code"]!=$sms_mobile_code)
         {
           return_data(40003,'验证码不正确');
         }
         else if($user_info["ctime"]<(gmtime()-900)) return_data(40003,'验证码已过期');

 }

    /**
     * 接口退出
     */
    public function logout() {
        $data['token']='';
        $where['user_id']=$this->user_id;
        $status=M('Users')->where($where)->save($data);
        if($status)
        {
          return_data(0,'退出登录');
        }
        else
        {
          return_data(10003,'退出登录失败');
        }
        
    }

        /**
     * 方法退出
     */
    protected function fn_logout() {
        $data['token']='';
        $where['user_id']=$this->user_id;
        $status=M('Users')->where($where)->save($data);
    }
    
 /*** 修改昵称*/
    public function edit_name(){
        /*************测试数据********************开始***/
       // $_POST['user_name']='123我上ee';
      /*************测试数据********************结束***/
      


       if ($_POST) {
              $Users=M('Users');

              $user_name = I('post.user_name','');
              if(!$user_name)
                return_data(20001,'昵称不能为空');
             
                      
              //重复昵称
              $where['user_name']=$user_name;
              $user_id=$Users->where($where)->getField('user_id');

              if( $user_id)
              {
                 return_data(30001,'昵称重复');
              }
            
              $where=array();
              $where['user_id'] = $this->user_id; 
              $data['user_name'] = $user_name;
              $status=$Users->where($where)->save($data);
              if ($status) {return_data(0,'昵称修改成功');} 
              else 
              return_data(10003,'昵称修改失败');
       }
       else
        {return_data(20001,'没有参数');}

     }
 /*** 修改密码*/
    public function edit_password() {
  /*************测试数据********************开始***/
       // $_POST['old_pwd']='123ggg';
       // $_POST['one_pwd']='123asd';
       // $_POST['sms_mobile_code']='1234556';
      /*************测试数据********************结束***/

       $info=$this->info;
       $Users=M('Users');
      // 手机找回密码处理
      if ($_POST) {              
          
          $old_pwd=I('post.old_pwd');
          $one_pwd=I('post.one_pwd');

          
          $where=array();
          $where['password']=md5(md5($old_pwd).$info['ec_salt']);
          $where['user_id']=$this->user_id;
          $status=$Users->where($where)->find();

         if ($status) {
              $where=array();
              $where['user_id']=$this->user_id;
              $ec_salt=rand(1000,9999);
              $data['password']=md5(md5($one_pwd).$ec_salt);
              $data['ec_salt']=$ec_salt;
              $status2=$Users->where($where)->save($data);
              if($status2){

                $this->fn_logout();

                return_data(0,'改密成功');
              }
              else return_data(10003,'改密失败');
        } 

        else 
        {
             return_data(20002,'旧密码错误');
        }
          
      }
      else
      {
         return_data(20001,'没有参数');
      }
    }

/*** 修改手机号*/
    public function edit_mobile() {
  /*************测试数据********************开始***/
       // $_POST['mobile_phone']='13758070545';
       // $_POST['old_pwd']='123asd';
      /*************测试数据********************结束***/

      // 手机找回密码处理
      if ($_POST) {
          $Users=M('Users');
          
          $mobile_phone=I('post.mobile_phone');
          $old_pwd=I('post.old_pwd');

          $sms_mobile_code=I('post.sms_mobile_code');
           //检测短信验证
          $this->check_sms_mobile_code($mobile_phone,$sms_mobile_code);

          
          if(!$mobile_phone)
          {
            return_data(20001,'手机不能为空');
          }



          $where=array();
          $where['mobile_phone'] =$mobile_phone;
          $check_user = $Users->where($where)->getField('user_id');
          if($check_user)
          {
            return_data(40004,'手机号已存在');
          }


          $where=array();
          $where['user_id'] =$this->user_id;
          $info = $Users->field('password,ec_salt')->where($where)->find();

          $pwd=md5(md5($old_pwd).$info['ec_salt']);
          if($info['password']!=$pwd)
          {
            return_data(20002,'密码有误');
          }
          
           $where=array();
           $where['user_id'] =$this->user_id;
           $data=array();
           $data['mobile_phone'] =$mobile_phone;
           $status = $Users->where($where)->save($data);
           if($status)
             return_data(0,'修改手机号成功');
           else
            return_data(10003,'修改手机号失败');
          
          
      }
      else
      {
         return_data(20001,'没有参数');
      }
    }
/*** 修改头像*/
    public function upload_user_img() {
  /*************测试数据********************开始***/
      /*************测试数据********************结束***/

      // 手机找回密码处理
      if (IS_POST) {
          
          if ($_FILES['user_img']['name'])
          {
                  $where=array();
                  $where['user_id'] = $this->user_id;
                  $old_img=M('users')->where($where)->getField('img');

                 
                  $info =upload_image('user_img', 'user_image');
                  if($info['error'])
                  {
                     return_data(30002 ,'头像更新失败',$info);
                  }
                  else
                  {
                    $img=$info['message'];
                  }
                  $data=array();
                  $data['img'] = $img;
                  $where=array();
                  $where['user_id'] = $this->user_id;
                  $status=M('Users')->where($where)->save($data);
               if($status)
               {
                 //删除过去的图片
                 $RATH=dirname(dirname(dirname(dirname(__FILE__))));

                 if($old_img)
                  @unlink($RATH.'/'.$old_img);
               
                $path='http://'.$_SERVER['HTTP_HOST'].'/'.__ROOT__.'/';
                $data=array();
                $data['img']=$img?$path.$img:'';


                return_data(0,'修改头像成功',$data);
               }
                 
               else
                return_data(10003,'修改头像失败');
          }
          else
          {
            return_data(20002,'头像没有上传');
          }
      
          
      }
      else
      {
         return_data(20001,'没有参数');
      }
    }


    /*选择旅行社*/
    public function chose_travel_agency()
    {
      /*************测试数据********************开始***/
      // $_POST['mshop_id']='2';
      /*************测试数据********************结束***/

     $data=$this->info;
     $DB_PREFIX=C('DB_PREFIX');
     //验证导游 是否当日选择旅行社
     if($data['end_mshop_time']>time())
     {
       return_data(30005,'当日旅行社有效，明日才能重新选择旅行社。');
     }

     //验证导游 
      if($data['utype']!=2)
      {return_data(30004,'导游才能选择旅行社');}

      if ($_POST) {

         $mshop_id=I('post.mshop_id');

         //检测mshop_id的合法性


         $now_time=gmtime();
         $end_mshop_time= strtotime(date('Y-m-d',strtotime('+1 day',$now_time)));
         $sql = 'update ' . $DB_PREFIX. "users set mshop_id ='$mshop_id',end_mshop_time=' $end_mshop_time' " . " WHERE user_id=" . $this->user_id;
         $status=M()->execute($sql);
         if($status)
         {
           //记录旅行社的名称
           $minfo=D('Mshop')->get_info($mshop_id);

           //记录导游何时选择旅行社
           $where_str="('$this->user_id','$mshop_id','$now_time')";
           $sql = 'insert into ' . $DB_PREFIX . "users_guide_chose_mshop(`user_id`,`mshop_id`,`ctime`)  values".$where_str;
            M()->execute($sql);
            
            $rn_data['mshop_name']=$minfo['mshop_name'];
            $rn_data['mshop_id']=$minfo['mshop_id'];
            return_data(0,'选择旅行社成功',$rn_data);
         }
         else
          return_data(10003,'选择旅行社失败');
      
          
      }
      else
      {
        return_data(20001,'没有参数');

      }



    }

    /*添加收货地址*/
   public function add_address()
   {
      /*************测试数据********************开始***/
      // $_POST['consignee']='zhsk';
      // $_POST['mobile_phone']='13758070546';
      // $_POST['country']='1';
      // $_POST['province']='2';
      // $_POST['city']='52';
      // $_POST['district']='500';
      // $_POST['address']='详细地址aaa';
      // $_POST['consignee']='Hao';
      // $_POST['zipcode']='314000';

       // $_POST['address_id']='18';

      //  print_r(json_encode($_POST));exit();
      /*************测试数据********************结束***/

        if ($_POST['city']) {
            $address = array(
                'user_id' => $this->user_id,
                'address_id' => intval(I('post.address_id')),
                'country' => I('post.country', 1, 'int'),
                'province' => I('post.province', 0, 'int'),
                'city' => I('post.city', 0, 'int'),
                'district' => I('post.district', 0, 'int'),
                'address' => I('post.address'),
                'consignee' => I('post.consignee'),
                'zipcode' => I('post.zipcode', ''),
                'mobile' => I('post.mobile_phone')
            );
            $address_id=D('Users')->update_address($address);

            if ($address_id) {
                 return_data(0,'添加收货地址成功',$address_id);
            }         
            else
            {
              return_data(10003,'添加收货地址失败');
            }
        }       
         else
        {
          return_data(20001,'没有参数');
        }
   }
    /*选择默认收货地址*/
   public function chose_user_address()
   {
      /*************测试数据********************开始***/
      // $_POST['address_id']='18';

       // print_r(json_encode($_POST));exit();
      /*************测试数据********************结束***/

      $address_id=I('post.address_id', 0, 'int');
      if (!$address_id) {
        return_data(20001,'用户收货地址参数为空');
      }
      $where['user_id'] = $this->user_id;
      $where['address_id'] = $address_id;
       //收货地址是否合法
       $u_address_id=M('user_address')->where($where)->getField('address_id');
       if (!$u_address_id) {
        return_data(20002,'收货地址不合法');
       }

        
        $where=array();
        $where['user_id'] = $this->user_id;
        $condition=array();
        $condition['address_id'] = $address_id;
        $status=M('users')->where($where)->save($condition);
        if ($address_id) {
             return_data(0,'选择默认收货地址成功',$address_id);
        }         
        else
        {
          return_data(10003,'选择默认收货地址失败');
        }

   }

  /*读取收货地址详情*/
   public function get_address()
   {
      /*************测试数据********************开始***/
      // $_POST['address_id']='18';

       // print_r(json_encode($_POST));exit();
      /*************测试数据********************结束***/

      $address_id=I('post.address_id', 0, 'int');
      if (!$address_id) {
        return_data(20001,'用户收货地址参数为空');
      }
      $where['user_id'] = $this->user_id;
      $where['address_id'] = $address_id;
      $field="consignee,mobile as mobile_phone,province,city,district,address,zipcode,address_id";
       //收货地址是否合法
       $u_address=M('user_address')->field($field)->where($where)->find();

        if ($u_address) {
             return_data(0,'收货地址详情读取成功',$u_address);
        }         
        else
        {
          return_data(10003,'收货地址详情读取失败');
        }

   }

   //获取区域
    public function get_regions()
    { 
      /*************测试数据********************开始***/
      // $_POST['region_type']='1';
      // $_POST['parent_id']='1';

       // print_r(json_encode($_POST));exit();
      /*************测试数据********************结束***/

      $DRegionBase=D('RegionBase');
       if ($_POST) {


            $region_type=I('post.region_type', 0, 'int');
            $parent_id=I('post.parent_id', 0, 'int');
            if($parent_id) $list=$DRegionBase->get_regions($region_type,$parent_id);
            else $list=$DRegionBase->get_regions($region_type);



            if ($list) {
                 return_data(0,'下拉地区成功',$list);
            }         
            else
            {
              return_data(10003,'下拉地区失败');
            }
        }       
         else
        {
          return_data(20001,'没有参数');
        }

     
    }

     //收货地址列表界面
    public function address_list()
    { 
       /*************测试数据********************开始***/
      // $_POST['last']='1';
      // $_POST['amount']='5';

       // print_r(json_encode($_POST));exit();
      /*************测试数据********************结束***/

      $DUsers=D('Users');
       if ($_POST) {


            $last=I('post.last', 1, 'int');//页数
            $limit=I('post.amount', 5, 'int');//大小


            $list=$DUsers->get_consignee_list($this->user_id, 0, $limit, ($last-1)*$limit);


            foreach ($list as $key => &$value) {
               // $value['consignee']=D('RegionBase')->get_region_name($value['consignee']);
               $value['province']=D('RegionBase')->get_region_name($value['province']);
               $value['city']=D('RegionBase')->get_region_name($value['city']);

               $value['district']=D('RegionBase')->get_region_name($value['district']);
               $value['mobile_phone']=$value['mobile'];
               unset($value['mobile']); 
            }


            $num=M('user_address')->where("user_id='$this->user_id'")->count();

            $page=get_page_list($list,$last,$limit,$num);


            if ($page['list']) {
                 return_data(0,'下拉地址列表成功',$page);
            }         
            else
            {
              return_data(0,'下拉地址列表为空',$page);
            }
        }       
         else
        {
          return_data(20001,'没有参数');
        }
    }
    /**
     * 删除收货地址
     */
    public function del_address_list() {
      /*************测试数据********************开始***/
      // $_POST['address_id']='19';

       // print_r(json_encode($_POST));exit();
      /*************测试数据********************结束***/

        $address_id=I('post.address_id', 0, 'int');
        $user_address=M('user_address');

        if ($_POST) {
          $where['user_id']=$this->user_id;
          $where['address_id']=$address_id;
          $u_address_id=$user_address->where($where)->getField('address_id');

          if($u_address_id)
          {
            $where=array();
            $where['address_id']=$address_id;
            $user_address->where($where)->delete();

            return_data(0,'删除收货地址成功'); 
          }
          else
            return_data(20002,'收货地址不是本人的');
        }
        else return_data(20001,'没有参数');
    }
  /**
     *订单列表
     */
    public function order_list() {
      /*************测试数据********************开始***/
      // $_POST['last']='1';
      // $_POST['amount']='0';
      // $_POST['type']='0';

       // print_r(json_encode($_GET));exit();
      /*************测试数据********************结束***/

      $last=I('post.last', 1, 'int');//页数
      $limit=I('post.amount',2, 'int');//大小

      $type=I('post.type',0, 'int');

      // $type            订单状态，0未付款，1全部，2待发货,3已发货,4退货，默认0
      $where='user_id = ' . $this->user_id;
      switch ($type) {
        case 1:
          break;
        case 2:
          $where.=" AND order_status IN ('1','5','6') AND "
                   ." shipping_status IN ('0','3','5') AND (pay_status IN ('2','1') ) and return_status=0";
          break;
       case 3:
          $where.=" AND order_status =5 and return_status=0 " .
               " AND shipping_status=1 ";
          break;
        case 4:
          $where.=" and (return_status>0 or order_status=4) ";
          break;
        
        default:
          $where.=" and order_status<2 and return_status=0 and pay_status =0 ";
          break;
      }

      // print_r($where);exit();


      $orders = D('Users')->get_user_orders($this->user_id, $type, $limit, ($last-1)*$limit);

      $count = M('order_info')->where($where)->count();
      $page=get_page_list($orders,$last,$limit,$count);



      if($page['list'])
      {
        return_data(0,'下拉订单成功',$page);
      }
      else
      {
         return_data(0,'没有订单',$page);
      }
    }

    /**
     * 取消订单
     */
    public function cancel_order() {
        /*************测试数据********************开始***/
      // $_POST['order_id']='96';
       // print_r(json_encode($_GET));exit();
      /*************测试数据********************结束***/

        $order_id = I('post.order_id', 0, 'int');
        $where_data['order_id']=$order_id;
        $where_data['user_id']= $this->user_id;
        $order=M("order_info")->field("user_id,order_id,order_sn,order_status,pay_status,bonus_id,return_status,surplus,integral")->where($where_data)->find();
        if(!$order)return_data(20002,'订单ID非法');

        if($order['return_status']>0)
        {
          return_data(20002,'审核中不能操作');
        }
        else if($order['pay_status']==2)
        {
          return_data(20002,'订单已付款，不可取消');
        }
        // if($order['order_status']==2 || $order['order_status']==4 || $order['order_status']==5)
        // {
        //   return_data(20002,'订单已取消');
        // }

        $up_data['return_status']=0;
        $up_data['order_status']=2;
        $up_data['bonus_id']=0;
        $up_data['bonus']=0;
        $up_data['integral']=0;
        $up_data['integral_money']=0;
        $up_data['surplus']=0;
        $status=M('order_info')->where($where_data)->save($up_data);

        if ($status) {
            /* 记录log */
            D('OrderBase')->order_action($order['order_sn'], 2, 0, 0, '取消订单', $this->user_id);

             /* 退货用户余额、积分、红包 */
            if ($order['user_id'] > 0 && $order['surplus'] > 0) {
                D('ClipsBase')->log_account_change($order['user_id'], $order['surplus'], 0, 0, 0, "取消账户金额，订单".$order['order_sn']);
            }
            if ($order['user_id'] > 0 && $order['integral'] > 0) {

                D('ClipsBase')->log_account_change($order['user_id'], 0, 0, 0, $order['integral'],  "取消积分，订单".$order['order_sn']);
            }
            if ($order['user_id'] > 0 && $order['bonus_id'] > 0) {
                D('Order')->change_user_bonus($order['bonus_id'], $order['order_id'], false);
            }

           /* 如果使用库存，且下订单时减库存，则增加库存 */
            $use_storage=M('shop_config')->where("code='use_storage'")->getField('value');
            $stock_dec_time=M('shop_config')->where("code='stock_dec_time'")->getField('value');


            if($use_storage=='1'&&$stock_dec_time=='1')
            D('Order')->change_order_goods_storage($order['order_id'], false, 1);
            
            return_data(0,'取消订单成功');


  
        } else {
           return_data(10003,'取消订单失败');
        }
    }

    /**
     * 确认订单
     */
    public function affirm_received() {
        /*************测试数据********************开始***/
      // $_POST['order_id']='135';
       // print_r(json_encode($_GET));exit();
      /*************测试数据********************结束***/

        $order_id = I('post.order_id', 0, 'int');
        $where_data['order_id']=$order_id;
        $where_data['user_id']= $this->user_id;
        $order=M("order_info")->field("commission,shipping_fee,mshop_id,user_id,money_paid,order_id,order_sn,order_status,pay_status,shipping_status,bonus_id,return_status,surplus,integral")->where($where_data)->find();
        $amount=$order['money_paid'];
        if(!$order)return_data(20002,'订单ID非法');

        if($order['return_status']>0)
        {
          return_data(20002,'审核中不能操作');
        }
        else if($order['pay_status']!=2)
        {
          return_data(20002,'订单未付款，不可确认收货');

        }
        else if($order['shipping_status']!=1)
        {
          return_data(20002,'订单发货状态未发货，不可确认收货');
        }

        $up_data['shipping_status']=2;
        $status=M('order_info')->where($where_data)->save($up_data);
        if ($status) {
            /* 记录log */
            D('OrderBase')->order_action($order['order_sn'], 2, 0, 0, '取消订单', $this->user_id);

             /* 检查：库存 */
           $use_storage=M('shop_config')->where("code='use_storage'")->getField('value');
           $stock_dec_time=M('shop_config')->where("code='stock_dec_time'")->getField('value');
           if($use_storage==1&&$stock_dec_time==0)
            D('Order')->change_order_goods_storage($order_id, true,0);

            //送积分-红包
             $will_get_integral = D('Order')->get_order_give_integral($order_id);
            if ($will_get_integral > 0) {

                D('ClipsBase')->log_account_change($this->user_id, 0, 0, 0, $will_get_integral,  "确认收货，积分累加，订单".$order['order_sn']);
            }
            
            //赠送红包--后台未查到有红包相关处理--退红包--暂时隐藏功能
            // $will_get_bonus_num = D('Order')->add_order_total_bonus($order_id,$this->user_id,$amount);


            //获得佣金_zhsf
            if($order['mshop_id']&&$amount)
            { 
                D('Mshop')->deal_profit($order['order_sn'],$order['mshop_id'],$amount,$order['shipping_fee'],$order['commission'],1);
            }

            return_data(0,'确认收货成功');

          }
          else
          {
            return_data(10003,'确认收货失败');
          }

    }

        /**
     * 删除订单--逻辑删除
     */
    public function del_order() {
   /*************测试数据********************开始***/
      // $_POST['order_id']='135';
       // print_r(json_encode($_GET));exit();
      /*************测试数据********************结束***/

        $order_id = I('post.order_id', 0, 'int');
        $where_data['order_id']=$order_id;
        $where_data['user_id']= $this->user_id;
        $order=M("order_info")->field("is_deleted,order_status,shipping_status,return_status")->where($where_data)->find();
        $amount=$order['money_paid'];
        if(!$order)return_data(20002,'订单ID非法');

        if($order['return_status']>0)
        {
          return_data(20002,'审核中不能操作');
        }
        if($order['is_deleted']==1)
        {
          return_data(10005,'订单已删除');
        }


        if($order['order_status']==2 ||($order['order_status']==5&&$order['shipping_status']==2) )//取消或确认收货完成
        {
             $up_data['is_deleted']=1;
             M("order_info")->where("order_id='$order_id'")->save($up_data);
             return_data(0,'订单删除成功');
        }
        else return_data(20002,'订单删除失败');
    }

        /**
     * 订单详情
     */
    public function order_detail() {


      /*************测试数据********************开始***/
      // $_POST['order_id']='34';
       // print_r(json_encode($_GET));exit();
      /*************测试数据********************结束***/

      $order_id=I('post.order_id',0, 'int');//订单ID
       // 订单详情
       $order = D('Users')->get_order_detail($order_id, $this->user_id);
       if(!$order)return_data(20002,'订单ID非法');

               //收货区域
        $province=D('RegionBase')->get_region_name($order['province']);
        $city=D('RegionBase')->get_region_name($order['city']);
        $district=D('RegionBase')->get_region_name($order['district']);
        unset($order['province']);
        unset($order['city']);
        unset($order['district']);

       $order['address']=$province.$city.$district.$order['address'];
       $DB_PREFIX=C('DB_PREFIX');


      
       $order['handler']=0;
       if($order['return_status']>0)
        {
           @$order['handler']=5;
        }
        else if ($order['order_status'] == 2 || $order['order_status'] == 4) {
           $order['handler']=4;
        } 
        else if ($order['order_status'] == 5) {
           $order['handler'] =3;

            /* 对配送状态的处理 */
            if ($order['shipping_status'] == 1) {
                 $shop_config=M('shop_config');
                $unfrozen_day=$shop_config->where("code='unfrozen_day'")->getField('value');
                $delivery_day=floatval($unfrozen_day)>0?$unfrozen_day:0;
                if(($order['return_status']==0)&&$delivery_day)
                {
                    //自动收货
                    $sh_time=$order['shipping_time']+3600*24*$delivery_day;
                    if($sh_time<=gmtime())
                    {

                        $sql=" update " . $DB_PREFIX ."order_info set shipping_status=2    where order_id='$order[order_id]'";
                        $res_sh = M()->execute($sql);
                        /* 记录日志 */
                        D('OrderBase')->order_action($order['order_sn'], $order['order_status'], 2, $order['pay_status'], '', $user_id);
                        //获得佣金_zhsf
                        $amount=$order['money_paid'];
                        if($order['mshop_id']&&$amount)
                        { 
                            
                            D('Mshop')->deal_profit($order['order_sn'],$order['mshop_id'],$amount,$order['shipping_fee'],$order['commission'],1);
                        }
                        $order['shipping_status']=2;
                        $order['handler'] =4;

                    }
                }


            }          
            elseif ($order['shipping_status'] == 2) {
                 $order['handler']=4;
            }
        }
       elseif ($order['shipping_status'] == 3) {
                 $order['handler']=2;
            }
        else {

                if ($order['pay_status'] == 2) {
                   $order['handler']=2;
                } else {
                   $order['handler']=1;
                }   
         }

        $goods=D('order')->order_goods($order['order_id']);
        $order['goods']=$goods;
        
        $order_num=0;
        foreach ($goods as  $gd_val) {
          $order_num+=$gd_val['goods_number'];
        }

        $order['goods_count']=$order_num;

        //处理order
        if($order['money_paid'])$order['order_sum_price']=$order['money_paid'];
        else $order['order_sum_price']=$order['order_amount'];

        unset($order['user_id']);
        unset($order['order_status']);
        unset($order['pay_status']);
        unset($order['shipping_status']);
        unset($order['return_status']);
        unset($order['pay_id']);
        // unset($order['pay_name']);
        unset($order['mobile_pay']);
        unset($order['mshop_id']);
        unset($order['commission']);
        // unset($order['order_amount']);
        unset($order['pay_fee']);
        // unset($order['bonus']);
        unset($order['money_paid']);
        unset($order['add_time']);
        unset($order['pay_time']);
        unset($order['shipping_time']);
        unset($order['total_fee']);


      if($order)
      {
        return_data(0,'订单详情显示成功',$order);
      }
      else
      {
         return_data(10003,'订单详情显示失败',$order);
      }



    }

     //获取红包列表
      public function get_use_bonus()
      {         
            /*************测试数据********************开始***/
            //  print_r(json_encode($_POST));exit();
            /*************测试数据********************结束***/
            $last=I('post.last', 1, 'int');//页数
            $limit=I('post.amount',10, 'int');//大小

            /* 如果使用红包，取得用户的红包 */
            $user_bonus=array();
            $user_bonus = D('users')->user_bonus_list($this->user_id,$limit,($last-1)*$limit);
            if (!empty($user_bonus)) {
                foreach ($user_bonus as $key => $val) {
                    $user_bonus[$key]['min_goods_amount']=round(floor($val ['min_goods_amount']),0);//向下四舍五入
                    $user_bonus[$key]['img_type_money']=round(floor($val ['type_money']),0);//向下四舍五入
                }
            }
            $num=M('user_bonus')->where("user_id='$this->user_id'")->count();
            
            $page=get_page_list($user_bonus,$last,$limit,$num);

            if($page['list'])
            {
               return_data(0,'显示用户可用红包成功',$page);
            }
            else
            {
              return_data(0,'用户没有可用红包',$page);
            }

            
      }

    //分享链接
    public function get_share_url()
    {
      $info=$this->info;
      $mshop_id=$info['mshop_id'];
      $user_id=$info['user_id'];

      // if($info['utype']!=2){return_data(20002,'非导游不能分享链接');}

      if($info['utype']==2&&$info['end_mshop_time']<time()&&$info['utype']==2){return_data(30003,'旅行社过期，重选旅行社');}

      if($info['utype']==2){$url_str="&user_pid=$user_id&mshopNo=$mshop_id";}
      else $url_str='';


      $re_data['share_url']=get_path_url()."mobile/index.php?m=default&c=user&a=register".$url_str;
      $share_url=M('shop_config')->where("code='share_url'")->getField('value');
      if($share_url)
      {
        $re_data['share_url']=$share_url.$url_str;
        return_data(0,'分享链接读取成功',$re_data);

      }
      else
      {
        return_data(0,'默认分享链接',$re_data);
      }
    }














    //查询微信订单订单状态
      public function check_wxorder_pay()
      {
         /*************测试*************开始***/
          // $_POST['order_id']=89;
          /*************测试*************结束***/
          require_once('WxMoneyPay.class.php');

           $order_id=I('post.order_id',0,'int');
           $user_id=$this->user_id;


           if(!$order_id)return_data(20001,'没有指向订单');
              
               //查看订单
           $order=M('order_info')->field("order_sn,order_amount,pay_status")->where("user_id='$user_id' and order_id='$order_id'")->find();
           if(!$order)return_data(20002,'用户不存在该订单');


           //判断订单情况
           if($order['pay_status']==2 || $order['order_amount']<=0) return_data(60000,'重复提交，订单已支付过(pay_status)');

           //判断重复提交支付
           $pay_log=M('pay_log')->field('log_id,order_amount,is_paid,transid')->where("discarded_status=0 and order_id='$order_id'")->find();

           //prepay_id 有2小时有效--可以进一步处理

           if($pay_log['transid'])
           {
              $log_id=$pay_log['log_id'];
              if($pay_log['is_paid']>0)
              {
                       return_data(60000,'重复提交，订单已支付过(is_paid)');
              }
              else if($pay_log['order_amount']!=$order['order_amount'])//危险操作
              {
              //   //订单价格变动--废弃订单
              //   $up_data['discarded_status']=1;
              //   $up_log=M('pay_log')->where("log_id='$log_id'")->save($up_data);
                return_data(60001,'订单与支付的金额不一致');
              }
           }
           else
           {
             return_data(20002,'订单未尝试微信支付过');
           }



              
           //参数处理
          $input['out_trade_no']=$order['order_sn'].'O'.$log_id;//订单号-必填


          // print_r($input);exit();

           //返回结果
            $result = WxMoneyPay::checkAppOrder($input);

               
              if($result['return_code']=="SUCCESS"&&$result['result_code']=="SUCCESS")
              {
                   $total_fee=$result['total_fee']/100;

                  if($input['out_trade_no']==$result['out_trade_no'])//订单号相同
                  {
                    if($total_fee==$pay_log['order_amount'])//支付金额相同
                    {
                       D('Payment')->order_paid($log_id, 2);
                       return_data(0,'更改订单支付状态成功',$result);
                    }
                  }
                  return_data(20002,'订单数据不匹配',$result);
                
              }
              else
              {
                 return_data(20002,'检测订单失败',$result);
              }

      }



}